# 查询参数和校验: 使用 Annotated[Query()]： Annotated 大法好，能用尽用
# 可选参数还要校验，用 Annotated，更全面的参数配置用 Query()， 类似的封装还有 Path() Body() Header() Cookie()

from typing import Annotated

from fastapi import FastAPI, Query

app = FastAPI()


@app.get("/items/")
async def read_items(
        # http://127.0.0.1:8000/items/?q1=111&q2=rick&q3=20241'
        q1: Annotated[str, Query(min_length=3)],  # q1必传
        q2: Annotated[str, Query()] = "rick",  # 开发时还是给可选参数一个默认值更少BUG
        q3: Annotated[str | None, Query(min_length=4, max_length=10, pattern="^2024$")] = None,  # 最少4最多10字符，且必须2024开头
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}, {"q1": q1}, {"q2": q2}, {"q3": q3}]}
    return results


@app.get("/items2/")
# 多个参数：随便传 感觉有点危险，list[str] 类型标注和校验是不太够的 后面也会上模型？
async def read_items(q: Annotated[list[str], Query()] = ["foo", "bar"]):
    query_items = {"q": q}
    return query_items


# 更友好的文档 title="标题怎么没显示" 后面再说现在不管
@app.get("/items3/", tags=["模块名"], summary="接口标题", response_description="接口详情")
async def read_items(
        q: Annotated[
            str | None,
            Query(
                alias="item-query",
                title="标题怎么没显示",
                description="描述",
                min_length=3,
                max_length=50,
                pattern="^2024$",
                deprecated=True,
                include_in_schema=True,
            ),
        ] = None,
):
    results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
    if q:
        results.update({"q": q})
    return results
